package com.swt.gap.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;

/**
 * @author HuGuowei
 */
public final class FileCharsetUtil {
    private FileCharsetUtil() {
    }

    /**
     * 猜文件编码
     *
     * @param sourceFile 文件路径
     * @return 编码名称
     */
    public static String getFilecharset(File sourceFile) {
        String charset = "GBK";
        byte[] first4Bytes = new byte[4];
        try {
            boolean checked = false;
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourceFile));
            bis.mark(0);
            int read = bis.read(first4Bytes, 0, 4);
            if (read == -1) {
                return charset;
            } else if ((first4Bytes[0] == (byte) 0x00) && (first4Bytes[1] == (byte) 0x00) &&
                    (first4Bytes[2] == (byte) 0xFE) && (first4Bytes[3] == (byte) 0xFF)) {
                charset = "UTF-32BE";
                checked = true;
            } else if ((first4Bytes[0] == (byte) 0xFF) && (first4Bytes[1] == (byte) 0xFE) &&
                    (first4Bytes[2] == (byte) 0x00) && (first4Bytes[3] == (byte) 0x00)) {
                charset = "UTF-32LE";
                checked = true;
            } else if ((first4Bytes[0] == (byte) 0xEF) && (first4Bytes[1] == (byte) 0xBB) &&
                    (first4Bytes[2] == (byte) 0xBF)) {
                charset = "UTF-8";
                checked = true;
            } else if ((first4Bytes[0] == (byte) 0xFE) && (first4Bytes[1] == (byte) 0xFF)) {
                charset = "UTF-16BE";
                checked = true;
            } else if ((first4Bytes[0] == (byte) 0xFF) && (first4Bytes[1] == (byte) 0xFE)) {
                charset = "UTF-16LE";
                checked = true;
            }
            bis.reset();
            if (!checked) {
                int loc = 0;
                while ((read = bis.read()) != -1) {
                    loc++;
                    if (read >= 0xF0) {
                        break;
                    }
                    if (0x80 <= read && read <= 0xBF) {
                        // 单独出现BF以下的，也算是GBK
                        break;
                    }
                    if (0xC0 <= read && read <= 0xDF) {
                        read = bis.read();
                        if (0x80 <= read && read <= 0xBF) {
                            // 双字节 (0xC0 - 0xDF)
                            // (0x80 - 0xBF),也可能在GBK编码内
                            continue;
                        } else {
                            break;
                        }
                    } else if (0xE0 <= read && read <= 0xEF) {
                        // 也有可能出错，但是几率较小
                        read = bis.read();
                        if (0x80 <= read && read <= 0xBF) {
                            read = bis.read();
                            if (0x80 <= read && read <= 0xBF) {
                                charset = "UTF-8";
                                break;
                            } else {
                                break;
                            }
                        } else {
                            break;
                        }
                    }
                }
            }
            bis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return charset;
    }
}
